<?php
/**
 * sdPHP
 * @author     码农<8044023@qq.com>
 * @copyright  sdPHP
 * @license    https://gitee.com/cq8044023/sdphp
 */
namespace sdphp\lib;
use sdphp\lib\CoreBase\PortManagr;
class SwooleServerWebSocket extends SwooleServer{
    public $_config;
    public function start(){
        $this->_config=$this->_portManagr->getPortConfig(PortManagr::SOCK_WS);
        $this->_server=new \swoole_websocket_server($this->_config["socket_name"], $this->_config["socket_port"]);
        $this->_server->on("open",[$this,"onOpen"]);
        $this->_server->on("request",[$this,"onRequest"]);
        $this->_server->on("message",[$this,"onMessage"]);
        $this->_server->on("close",[$this,"onClose"]);
        $this->_server->set($this->_config["server_config"]);
        (new SwooleServerTcp())->start($this);
        $this->_server->start();
    }
    //ws
    public function onMessage($server, $frame){
        echo "1";
    }
    //启动
    public function onOpen($server, $request){
        echo "server: handshake success with fd{$request->fd}\n";
    }
    //http
    public function onRequest($request, $response){
        $route_obj=$this->_route->start($this->_config["route_tool"])->handleClientRequest($request->get, PortManagr::SOCK_WS);
        if (!$route_obj->is_status){//重定向 404
            echo "重定向404";
        }
        //TODO 待写验证 文件 类 方法
        $stdObj=new \stdClass();
        $stdObj->request  =$request;
        $stdObj->response =$response;
        $stdObj->sock     =PortManagr::SOCK_WS;
        $obj_new="\app\\{$route_obj->getModel()}\Controller\\{$route_obj->getController()}Controller";
        $controller=new $obj_new($stdObj);
        $method=$this->_config["http_method_prefix"].$route_obj->getMethod();
        $controller->$method();
    }
    //关闭
    public function onClose($ser, $fd){
        echo "client {$fd} closed\n";
    }
    
}

?>